<?php

namespace plugin\admin\app\controller;

use support\Request;
use support\Response;
use plugin\admin\app\model\RlBanner;
use support\exception\BusinessException;

/**
 * 轮播管理 
 */
class RlBannerController extends Crud
{
    
    /**
     * @var RlBanner
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlBanner;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-banner/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $bannerTitle=$request->post('banner_title','');
            $bannerIcon=$request->post('banner_icon','');
            $sort=$request->post('sort',0)?:0;
            $bannerType=$request->post('banner_type',0)?:0;
            $bannerValue=$request->post('banner_value',0)?:0;
            $ret=RlBanner::insert([
                'banner_title'=>$bannerTitle,
                'banner_icon'=>$bannerIcon,
                'sort'=>$sort,
                'banner_type'=>$bannerType,
                'banner_value'=>$bannerValue,
                'create_time'=>time()
            ]);
            if (!$ret){
                return $this->json(-1);
            }
            return $this->json(0);
        }
        return view('rl-banner/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $request->update_time=time();
            return parent::update($request);
        }
        return view('rl-banner/update');
    }

}
